﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Application;
using Business;
using Framework;

namespace UserControls
{
    public partial class GridViewManager : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            populateGridView();
        }

        protected void populateGridView()
        {
            IList<VONews> news = BusinessNews.GetAllNews();
            GVManager.DataSource = news;
            GVManager.DataBind();
        }


        protected void GVManager_PageIndexChanging(object sender,  GridViewPageEventArgs e)
        {
            GVManager.PageIndex = e.NewPageIndex;
            populateGridView();
        }

        protected void GVManager_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewRow row = GVManager.Rows[e.NewEditIndex];
            string id = ((Label)row.Cells[0].Controls[1]).Text;
            string redirectUrl = string.Format("/PageLayout/NewsPageLayout.aspx?id={0}", id);
            string scriptRedirect = string.Format("window.open('{0}', '_blank')", redirectUrl);
            Page.ClientScript.RegisterStartupScript(this.GetType(), null, scriptRedirect, true);
        }

        protected void GVManager_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        Panel pnlDetail = new Panel();
                        pnlDetail.CssClass = "detailedPanel";
                        TableCell detailedCell = new TableCell();
                        detailedCell.Controls.Add(pnlDetail);
                        int colSpan = e.Row.Cells.Count;
                        detailedCell.Attributes["ColSpan"] = MappingTool.CatchNull(colSpan);
                        
                        GridViewRow gRow = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
                        gRow.Cells.Add(detailedCell);
                        gRow.CssClass = "hiddenRow";
                        Table tbl = (Table)e.Row.Parent;
                        tbl.Controls.Add(gRow);
                        break;
                    }
                case DataControlRowType.Pager:
                    {
                        TableCell pagerCell = e.Row.Cells[0];
                        pagerCell.CssClass = "pagerclass";
                        pagerCell.ColumnSpan--;
                        ((Table)pagerCell.Controls[0]).Rows[0].Cells[GVManager.PageIndex].CssClass = "currentFrame";
                        e.Row.Cells.Add(new TableCell());
                        TableCell addButtCell = e.Row.Cells[1];
                        addButtCell.CssClass = "footerstyle center";
                        ImageButton imgAdd = new ImageButton();
                        imgAdd.ImageUrl = "/Images/Test/1362095942_round_plus.png";
                        imgAdd.CssClass = "plus-icon";
                        addButtCell.Controls.Add(imgAdd);
                        break;
                    }
            }
        }
    }
}